/*
 * (C) Copyright 2013 George Daswani (georgedaswani@gmail.com) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *    George Daswani (georgedaswani@gmail.com)
 */

package am.api.wrapper;

import javax.transaction.xa.XAResource;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import am.api.AMConnection;
import am.api.XAAMConnection;
import am.api.exception.AMConnectionException;

public class XAAMConnectionImpl implements XAAMConnection {

	private final AMConnection connection;
	private final XAResource resource;

	public XAAMConnectionImpl(AMConnection connection, XAResource resource) {
		super();
		this.connection = connection;
		this.resource = resource;
	}

	@Override
	public XAResource getXAResource() {
		return resource;
	}

	@Override
	public void close() {
		connection.close();
	}

	@Override
	public AMConnection getConnection() throws AMConnectionException {
		return connection;
	}

	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		XAAMConnectionImpl rhs = (XAAMConnectionImpl) obj;
		return new EqualsBuilder().append(connection, rhs.connection)
				.append(resource, rhs.resource).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder(103, 105).append(connection)
				.append(resource).toHashCode();
	}

}
